/*
 * Converter.h
 *
 *  Created on: Oct 28, 2011
 *      Author: Philip Rodriguez, Alfred Lee, Oliver Wing
 *
 *  Convert analog signal from the signal generator to digital data
 */

#ifndef CONVERTER_H_
#define CONVERTER_H_

#include <stdint.h>       /* for uintptr_t */
#include "types.h"

// Base address to the A/D converter
#define BASE_ADDRESS					(0x280)
// Base address to the least significant byte
// of the digital data
#define LSB_BASE_ADDRESS				(BASE_ADDRESS)
// Base address to the most significant byte
// of the digital data
#define MSB_BASE_ADDRESS				(BASE_ADDRESS + 0x01)
// What to write to the base address to start the
// A/D conversion
#define BASE_OUTPUT_VAL					(0x80)
// The address to an A/D channel
#define AD_CHANNEL_ADDRESS				(BASE_ADDRESS + 0x02)
// Writing to the A/D channel to specify the channel
#define AD_CHANNEL_VAL					(0x44)
// The address to the analog input status address
#define ANALOG_INPUT_STATUS_ADDRESS		(BASE_ADDRESS + 0x03)
// Writing to the analog input status address to
// specify the range of the input voltage
#define ANALOG_INPUT_STATUS_VAL			(0x00)
// The bit to wait on for the conversion to finish
#define ANALOG_INPUT_WAIT_BIT			(0b00100000)
// How long to wait for the conversion to finish
// The increment wait is the number of usec to sleep between timer updates
#define AD_INCREMENT_TIMEOUT_WAIT		(10)
// The duration is the number of updates before it's too late
#define AD_CONVERSION_TIMEOUT_DURATION	(20000 / AD_INCREMENT_TIMEOUT_WAIT)
// The absolute value of the minimum/maximum value
// of the input voltage in the form of digital data
#define AD_RANGE						(32768)
// The absolute value of the minimum/maximum value
// of the input voltage in volts
#define INPUT_RANGE						(10)
// The absolute value of the minimum/maximum value
// of the allowable range of voltage in volts
#define ALLOWABLE_RANGE					(5)
// The absolute value of the minimum/maximum value
// of the allowable voltage in the form of digital data
#define AD_ALLOWABLE_RANGE				((AD_RANGE * ALLOWABLE_RANGE) / INPUT_RANGE)
// The ASCII value of the bell noise to emit
// if there is an error on QNX
#define BELL_NOISE_CHAR					(0x07)

// Handles to the addresses necessary to perform
// an A/D conversion
uintptr_t lsbHandle;
uintptr_t msbHandle;
uintptr_t adChannelHandle;
uintptr_t analogInputStatusHandle;

// Setting up the handles to the A/D addresses
// and readying the hardware for A/D conversions
void initADRegister(void);

// Quasi-POST test to verify that the DAQ is able to make an A/D conversion
bool POSTInputDAQ(void);

// Performing an A/D conversion by waiting
// for the hardware to finish the conversion
bool performADConversion(void);

// Getting the 16-bit digital data from the A/D
// conversion
INT16 readADData(void);

// Local, utility function to convert the input A/D reading to volts
float convertADData(INT16);

#endif /* CONVERTER_H_ */
